import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: () => import("../components/login.vue"),
  },
  {
    path: "/home",
    component: () => import("../components/home.vue"),
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: () => import("../components/welcome.vue"),
      },
      {
        path:'/users',
        component:() => import('../components/user/users.vue')
      }
    ],
  }, 
];

const router = new VueRouter({
  routes,
});
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to将要访问的路径
  //from从哪个路径来
  //next一个函数表示放行
  if (to.path === "/login") return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) return next("/login");
  next();
});
export default router;
